//
//  WorkTableViewCell.m
//  Rail
//
//  Created by hychen on 2018/3/1.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "WorkTableViewCell.h"
#import "SCPieChart.h"

#define kAllDeviceColor Color_RGB(0, 193, 224)
#define kNormalDeviceColor Color_RGB(74, 225, 34)
#define kUnusualDeviceColor Color_RGB(255, 91, 102)

@interface WorkTableViewCell ()
@property (weak, nonatomic) IBOutlet UIView *circleContainer;
@property (weak, nonatomic) IBOutlet UILabel *workLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UIButton *refreshButton;
@property (weak, nonatomic) IBOutlet UIButton *favButton;
@property (weak, nonatomic) IBOutlet UIButton *detailButton;
@property (weak, nonatomic) IBOutlet UILabel *AllDeviceNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *normalDeviceNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *unnormalDeviceNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *onlineNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *workNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *supervisorNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *manageNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *proprietorNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *otherNumLabel;

@property (nonatomic, strong) UIButton *moreButton;

@property (nonatomic, strong) SCPieChart *pieChart;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *countViewBottomConstraints;

@property (nonatomic, copy) void(^BlockClickMore)(void);
@end

@implementation WorkTableViewCell

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        UIButton *moreBtn = [UIButton buttonWithTitle:@"查看更多" titleColor:Color_RGB(86, 86, 86) norImage:nil highImage:nil action:@selector(clickMoreButton) owner:self];
        [moreBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
        self.moreButton = moreBtn;
        [self addSubview:moreBtn];
        moreBtn.hidden = YES;
        [moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(35);
            make.left.right.equalTo(self);
            make.bottom.mas_equalTo(-10);
        }];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)hideMore {
    self.countViewBottomConstraints.constant = 10;
    self.moreButton.hidden = YES;
}

- (void)showMore:(void(^)())block {
    self.countViewBottomConstraints.constant = 45.f;
    self.moreButton.hidden = NO;
    self.BlockClickMore = block;
}

- (void)clickMoreButton {
    if (self.BlockClickMore) {
        self.BlockClickMore();
    }
}

- (IBAction)refreshBtnClick:(UIButton *)sender {
    if (self.BlockClickRefresh) {
        self.BlockClickRefresh(self.model,self.indexPath);
    }
}

- (IBAction)favBtnCLick:(id)sender {
    if (self.BlockClickFav) {
        self.BlockClickFav(self.model,sender);
    }
}

- (IBAction)detailBtnClick:(id)sender {
    if (self.BlockClickXiangqing) {
        self.BlockClickXiangqing(self.model);
    }
}

- (void)configPieChart {
    if (self.pieChart.superview) {
        [self.pieChart removeFromSuperview];
        self.pieChart = nil;
    }

    NSArray *items = @[[SCPieChartDataItem dataItemWithValue:[self.model.device_count integerValue] color:kAllDeviceColor description:@""],//全部
                       [SCPieChartDataItem dataItemWithValue:[self.model.device_online_count integerValue] color:kNormalDeviceColor description:@""],//正常
                       [SCPieChartDataItem dataItemWithValue:[self.model.device_offline_count integerValue] color:kUnusualDeviceColor description:@""],//异常
                       ];
    
    self.pieChart = [[SCPieChart alloc] initWithFrame:CGRectMake(0, 0, 85.0, 85.0) items:items];
    self.pieChart.showDescValues = NO;
    self.pieChart.descriptionTextColor = kPieChartDescColor;
    self.pieChart.descriptionTextFont  = [UIFont fontWithName:@"Avenir-Medium" size:12.0];
    [self.pieChart strokeChart];
    [self.circleContainer addSubview:self.pieChart];
    [self.pieChart mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.circleContainer);
    }];
}

- (void)setModel:(WorkAreaModel *)model {
    _model = model;
    
    [self configPieChart];
    self.workLabel.text = [NSString stringWithFormat:@"%@ %@",model.area_name?:@"",[WarnLevelConfig workAreaStatusString:model.status]?:@""];
    self.AllDeviceNumLabel.text = model.device_count?:@"0";
    self.normalDeviceNumLabel.text = model.device_online_count?:@"0";
    self.unnormalDeviceNumLabel.text = model.device_offline_count?:@"0";
    self.timeLabel.text = [BMUtils chineseMMDDHHmmWithServiceTime:model.op_time];
    self.onlineNumLabel.text = model.online_count?:@"0";
    self.workNumLabel.text = model.workuser_count?:@"0";
    self.supervisorNumLabel.text = model.supervisor_count?:@"0";
    self.manageNumLabel.text = model.manger_count?:@"0";
    self.proprietorNumLabel.text = model.owner_count?:@"0";
    self.otherNumLabel.text = model.other_count?:@"0";
    
    self.favButton.selected = model.is_fav;
}
@end
